package m.stock.v1;

import org.apache.commons.io.FileUtils;
import org.apache.commons.math3.util.Precision;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by maojianfeng on 8/4/16.
 */
public class Parser {
    private static Logger logger = Logger.getLogger(Parser.class);

    public static List<StockRecord> parse(File candidateFile){
        List<StockRecord> list = new ArrayList<StockRecord>();
        try {
            String fileContent = FileUtils.readFileToString(candidateFile,"utf-8");
            Document doc = Jsoup.parse(fileContent);
            Elements tables = doc.getElementsByTag("table");
            if(tables!=null&&!tables.isEmpty()){
//                logger.debug(tables.size());
                Element table = tables.first();
                Elements trs = table.getElementsByTag("tr");
    //                logger.debug(trs.size());
                if(trs!=null&&trs.size()>1){
                    Element header = trs.first();
//                    logger.debug(header);
                    for(int i=1;i<trs.size();i++){
                        Element tr = trs.get(i);
                        Elements tds = tr.getElementsByTag("td");
                        if(tds!=null&&tds.size()==15){
                            StockRecord record = new StockRecord();
                            record.setDate(candidateFile.getName().replace(".xls",""));
                            record.setStockCode(tds.get(0).text());
                            record.setStockAbbrName(tds.get(1).text());
                            record.setChg(toDouble(tds.get(2).text()));
                            record.setPrice(toDouble(tds.get(3).text()));
                            record.setMacdDiff(toDouble(tds.get(4).text()));
                            record.setMacdDea(toDouble(tds.get(5).text()));
                            record.setOpeningPrice(toDouble(tds.get(6).text()));
                            record.setClosingPrice(toDouble(tds.get(7).text()));
                            record.setTradingVolumn(toDouble(tds.get(8).text()));
//                            record.setBuySignal(tds.get(9).text());
//                            record.setLastShape(tds.get(10).text());
//                            record.setChgBefore(toDouble(tds.get(11).text()));
//                            record.setChgMoney(toDouble(tds.get(12).text()));
//                            record.setAmplitude(toDouble(tds.get(13).text()));
//                            record.setDealMoney(toDouble(tds.get(14).text()));
                            record.setMacdComputed(Precision.round((record.getMacdDiff()-record.getMacdDea())*2,2));
                            list.add(record);
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return list;
    }
    private static Double toDouble(String str){
        Double d = 0.00D;
        try {
            d = Double.valueOf(str);
        } catch (NumberFormatException e) {
            d = 0.00D;
        }
        return d;
    }
}
